from xml.etree.ElementTree import Element, SubElement, tostring
from windoh.style import styledict

def serialize_style(style):
    ret = []
    for k, v in style.iteritems():
        if type(v) is float:
            v = "%.5f" % v
        else:
            v = str(v)
        ret.append("%s: %s" % (k, v))
    return "; ".join(ret)

def serialize_control(control):
    e = Element(control.__class__.__name__)
    #if control.name:
    #    e.attrib['name'] = control.name
    #if control.classname:
    #    e.attrib['classname'] = control.classname
    #if control.style:
    #    e.attrib['style'] = serialize_style(control.style)
    e.attrib.update(control.__getstate__())

    for k, v in e.attrib.iteritems():
        if v is None:
            e.attrib[k] = "(none)"
        elif isinstance(v, styledict):
            e.attrib[k] = serialize_style(v)
        else:
            e.attrib[k] = str(v)
            

    for child in control._children_proxy:
        assert(child is not None)
        ce = serialize_control(child)
        e.append(ce)
    return e

def dump(control):
    return tostring(serialize_control(control))



